package com.example.calories

import androidx.appcompat.app.AppCompatActivity

import android.content.ContentValues
import android.content.Intent
import android.database.Cursor
import android.database.sqlite.SQLiteDatabase
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.Toast

import com.jjoe64.graphview.GraphView
import com.jjoe64.graphview.series.DataPoint
import com.jjoe64.graphview.series.LineGraphSeries

import java.sql.SQLOutput
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date
import com.example.calories.DatabaseHelper as DatabaseHelper1

class MainActivity : AppCompatActivity(), View.OnClickListener {
    private var eidt: Button? = null
    private var click1: Button? = null
    private var click2: Button? = null
    private var query: Button? = null
    private var filterDate: Button? = null
    private var editText: EditText? =null
    private var editText1: EditText? =null
    private var editText2: EditText? =null
    val time: String
        get() {
            val df = SimpleDateFormat("yyyy-MM-dd")
            var time = df.format(Date())
            //println(df.format(Date()))
            val weekDays = arrayOf("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六")
            val cal = Calendar.getInstance()
            cal.time = Date()
            var w = cal.get(Calendar.DAY_OF_WEEK) - 1
            if (w < 0) w = 0
            time = time + "-" + "" + weekDays[w]
            return time
        }

    // CalendarViews calendarViews;
    // Bundle bundle;
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        // bundle = getIntent().getBundleExtra("bundle");
        init()
    }

    fun init() {
        //calendarViews = new CalendarViews();
        eidt = findViewById(R.id.save_text)
        click1 = findViewById(R.id.click_1)
        click2 = findViewById(R.id.click_2)
        query = findViewById(R.id.query)
        filterDate = findViewById(R.id.filterDate)
        eidt!!.setOnClickListener(this)
        click1!!.setOnClickListener(this)
        click2!!.setOnClickListener(this)
        query!!.setOnClickListener(this)
        filterDate!!.setOnClickListener(this)
        editText = findViewById(R.id.text)//保存卡路里
        editText1 = findViewById(R.id.begin)//开始
        editText2 = findViewById(R.id.end)//结束
        //        GraphView graph = (GraphView) findViewById(R.id.graph);
        //        LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(new DataPoint[]{
        //                new DataPoint(0, 1),
        //                new DataPoint(1, 5),
        //                new DataPoint(2, 3),
        //                new DataPoint(3, 2),
        //                new DataPoint(4, 6)
        //        });
        //        graph.addSeries(series);

        //    生成图标，按照最近录入的卡路里
        val databaseHelper = DatabaseHelper1(this, "EX2", null, 1)
        val db = databaseHelper.writableDatabase
        val graph = findViewById<View>(R.id.graph) as GraphView
        val point = IntArray(10)
        for (w in 0..9) point[w] = 0
        val cursor = db.query("calories", null, null, null, null, null, null)
        var i = 0
        while (cursor.moveToNext()) {
            val calorie = cursor.getString(cursor.getColumnIndex("calorie"))
            //String times = cursor.getString(cursor.getColumnIndex("times"));
            point[i] = Integer.parseInt(calorie)
           // println(point[i])
            i++
            if (i >= 9) break
        }
        val series = LineGraphSeries(arrayOf(DataPoint(0.0, point[0].toDouble()), DataPoint(1.0, point[1].toDouble()), DataPoint(2.0, point[2].toDouble()), DataPoint(3.0, point[3].toDouble()), DataPoint(4.0, point[4].toDouble()), DataPoint(5.0, point[5].toDouble()), DataPoint(6.0, point[6].toDouble()), DataPoint(7.0, point[7].toDouble()), DataPoint(8.0, point[8].toDouble()), DataPoint(9.0, point[9].toDouble())))
        graph.addSeries(series)
    }

    override fun onClick(view: View) {
        /*
         *先获取卡路里信息保存后跳转
         * */
        //val insertCalorie = editText.text.toString()
        val insertCalorie = editText?.text.toString()
        val findText1 = editText1?.text.toString()
        val findText2 = editText2?.text.toString()
        val databaseHelper = DatabaseHelper1(this, "EX2", null, 1)
        val db = databaseHelper.writableDatabase
        when (view.id) {

            R.id.save_text -> {
                val values = ContentValues()
                val intent = Intent(this@MainActivity, Main2Activity::class.java)
                if (insertCalorie.isEmpty()) {
                    Toast.makeText(this@MainActivity, "未输入卡路里", Toast.LENGTH_LONG).show()
                } else {
                    values.put("calorie", insertCalorie)
                    values.put("times", time)
                    db.insert("calories", null, values)
                }
                startActivity(intent)
            }
            R.id.click_1 -> {
                val intent1 = Intent(this@MainActivity, CalendarViews::class.java)
                startActivity(intent1)
            }
            R.id.click_2 -> {
                val intent2 = Intent(this@MainActivity, CalendarViews::class.java)
                startActivity(intent2)
            }
            R.id.query -> {
                val whereClaus = "times like '%1%'"
                db.delete("calories", whereClaus, null)
            }
            R.id.filterDate -> if (findText1.isBlank() || findText2.isBlank()) {
                Toast.makeText(this@MainActivity, "请输入日期", Toast.LENGTH_SHORT).show()
            } else {
                val intent3 = Intent(this@MainActivity, Main3Activity::class.java)
                val bundle = Bundle()
                bundle.putString("begin", findText1)
                bundle.putString("end", findText2)
                intent3.putExtra("bundle", bundle)
                startActivity(intent3)
            }
        }

    }
}